package empacotador;

public class UnsignedByte {
	
  private int value;

  UnsignedByte(byte b)
  {
    value = toUnsigned(b);
  }

  public int getValue()
  {
    return value;
  }

	/**
	 * signed byte para unsigned byte
	 * @param b
	 * @return
	 */
	public static int toUnsigned(byte b)
	{
		return (int) (b & 0xff);
	}

	public static char toUnsignedNovo(byte b)
	{
		return (char) (b & 0xff);
	}


  /**
   * unsigned byte para signed byte
   * @param i
   * @return
   */
  public static byte toSigned(int i)
  {
    return (byte) i;
  }

  public static int[] toUnsigned(byte[] signeds)
  {
    int[] unsigneds = new int[signeds.length];
    for(int i =0; i<signeds.length; i++)
      unsigneds[i] = UnsignedByte.toUnsigned(signeds[i]);

    return unsigneds;
  }

  public static char[] toUnsigned(byte[] signeds, int start, int offset)
  {

    if ((start+offset) > signeds.length || start > signeds.length)
      throw new IndexOutOfBoundsException();

    char[] unsigneds = new char[offset];
    for(int i=start; i<(start+offset); i++)
      unsigneds[i] = UnsignedByte.toUnsignedNovo(signeds[i]);

    return unsigneds;
  }

  public static byte[] toSigned(int[] unsigneds)
  {
    byte[] signeds = new byte[unsigneds.length];
    for(int i=0; i<unsigneds.length; i++)
      signeds[i] = UnsignedByte.toSigned(unsigneds[i]);


    return signeds;
  }

}
